package com.example.vaultmysql.dto;

import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * Vault连接测试结果DTO
 * 
 * @author System
 * @version 1.0.0
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class VaultConnectionTestResult {

    /**
     * 连接测试是否成功
     */
    private Boolean success;

    /**
     * 连接结果消息
     */
    private String message;

    /**
     * 连接延迟（毫秒）
     */
    private Long latency;

    /**
     * 测试时间戳
     */
    private Long timestamp;

    /**
     * Vault服务器地址
     */
    private String vaultUrl;

    /**
     * 错误代码
     */
    private String errorCode;

    /**
     * 构造函数
     */
    public VaultConnectionTestResult(Boolean success, String message, Long latency) {
        this.success = success;
        this.message = message;
        this.latency = latency;
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 创建成功结果
     */
    public static VaultConnectionTestResult success(String message, Long latency) {
        return VaultConnectionTestResult.builder()
                .success(true)
                .message(message)
                .latency(latency)
                .timestamp(System.currentTimeMillis())
                .build();
    }

    /**
     * 创建失败结果
     */
    public static VaultConnectionTestResult failure(String message, Long latency) {
        return VaultConnectionTestResult.builder()
                .success(false)
                .message(message)
                .latency(latency)
                .timestamp(System.currentTimeMillis())
                .build();
    }

    /**
     * 创建失败结果（带错误代码）
     */
    public static VaultConnectionTestResult failure(String message, Long latency, String errorCode) {
        return VaultConnectionTestResult.builder()
                .success(false)
                .message(message)
                .latency(latency)
                .errorCode(errorCode)
                .timestamp(System.currentTimeMillis())
                .build();
    }

    /**
     * 获取延迟等级描述
     */
    public String getLatencyLevel() {
        if (latency == null) {
            return "未知";
        }
        
        if (latency < 100) {
            return "优秀";
        } else if (latency < 500) {
            return "良好";
        } else if (latency < 1000) {
            return "一般";
        } else {
            return "较慢";
        }
    }

    /**
     * 检查是否成功
     */
    public boolean isSuccess() {
        return success != null && success;
    }

    /**
     * 检查是否失败
     */
    public boolean isFailure() {
        return !isSuccess();
    }
}